﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace SedCup.Code.Model
{
    public class SearchForJob
    {
        public static List<Task> SearchForNearestJob(Dictionary<int, int> meList, Dictionary<int, int> enemyList, List<Task> list)
        {
            List<Task> listNear = new List<Task>();

            foreach (Task task in list)
            {
                if (meList[task.coordinate] < enemyList[task.coordinate])
                {
                    listNear.Add(task);
                }
            }

            return listNear;
        }



        public static Dictionary<string, List<Task>> DetermineWinnerStrategy(ServerResponse response)
        {
            Dictionary<string, List<Task>> rvResult = new Dictionary<string, List<Task>>();
            List<Task> usedTasks = new List<Task>();
            List<Headquarter> headquarters = response.headQuarters;

            rvResult["junior"] = new List<Task>();
            rvResult["senior"] = new List<Task>();
            rvResult["guru"] = new List<Task>();

            Headquarter myHeadQuarter;
            Headquarter enemyHeadQuarter;
            if (headquarters[0].owner.Equals("me"))
            {
                myHeadQuarter = headquarters[0];
                enemyHeadQuarter = headquarters[1];
            }
            else
            {
                myHeadQuarter = headquarters[1];
                enemyHeadQuarter = headquarters[0];
            }

            Dictionary<int, int> meList = Neighbour.determineAllDistancesFromPosition(myHeadQuarter.coordinate, response.mapSize, response.roadBlocks);
            Dictionary<int, int> enemyList = Neighbour.determineAllDistancesFromPosition(enemyHeadQuarter.coordinate, response.mapSize, response.roadBlocks);

            List<Task> tasks = SearchForNearestJob(meList, enemyList, response.tasks);
            int junior = 2;
            int senior = 5;
            int guru = 3;

            int juniorSalary = 3;
            int seniorSalary = 5;
            int guruSalary = 8;

            while (true)
            {
                int profit = Int32.MinValue;
                int whodoesit = -1;
                int task = -1;

                for (int i = 0; i < tasks.Count; i++)
                {
                    if(usedTasks.Contains(tasks[i]))
                    {
                        continue;
                    }

                    if (tasks[i].type.Equals("c++"))
                    {
                        if (junior > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, juniorSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 0; // 0 is junior
                            }
                        }
                        else if (senior > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, seniorSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 1; // 1 is senior
                            }
                        }
                        else if (guru > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, guruSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 2; // 2 is guru
                            }
                        }
                    }

                    if (tasks[i].type.Equals("java"))
                    {
                        if (junior > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, juniorSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 0; // 0 is junior
                            }
                        }
                        else if (guru > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, guruSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 2; // 2 is guru
                            }
                        }
                    }

                    if (tasks[i].type.Equals("c#"))
                    {
                        if (senior > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, seniorSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 1; // 1 is senior
                            }
                        }
                        else if (guru > 0)
                        {
                            int actualProfit = Neighbour.profitFromTask(meList, tasks[i], response.turn, guruSalary);
                            if (actualProfit > profit)
                            {
                                profit = actualProfit;
                                task = i;
                                whodoesit = 2; // 2 is guru
                            }
                        }
                    }
                }

                if (profit > 0)
                {
                    usedTasks.Add(tasks[task]);

                    switch (whodoesit)
                    {
                        case 0:
                            rvResult["junior"].Add(tasks[task]);
                            junior--;
                            break;
                        case 1:
                            rvResult["senior"].Add(tasks[task]);
                            senior--;
                            break;
                        case 2:
                            rvResult["guru"].Add(tasks[task]);
                            guru--;
                            break;
                    }
                }
                else
                {
                    break;
                }
            }

            return rvResult;
        }
    }
}
